# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=onevpl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2023.1.1
pkgrel=1
pkgdesc="oneAPI Video Processing Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.intel.com/content/www/us/en/developer/tools/oneapi/onevpl.html'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/oneapi-src/oneVPL/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-fix-pkgconfig-file.patch
        0002-cmake-install-vars-sh.patch
        0003-cmake-fix-32bit-install.patch)
sha256sums=('f3973f98b08564fd529079911c4e64cd5b552f6933a4a71d085609db6e5de09a'
            '1558d30f950e7ef33f06540fcb76ceccdd4a076eeee747da80be681a15191d2d'
            '37114e582e91e9a1788bf7dea180e831876e0a9c28173f87a7f4dc8daa79f427'
            'd93c7647c6756a02f6ca7d56165178ea6d78ec5f696aefacdd8ffda420486ea0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    0001-fix-pkgconfig-file.patch \
    0002-cmake-install-vars-sh.patch \
    0003-cmake-fix-32bit-install.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DBUILD_TOOLS=OFF \
      -DINSTALL_EXAMPLE_CODE=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TOOLS=OFF \
      -DINSTALL_EXAMPLE_CODE=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
